

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.backends.androidx;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnGenericMotionListener;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

/** Subclass of AndroidInput, used on Android +3.x to get generic motion events for things like gamepads/joysticks and so on.
 * @author mzechner */
public class AndroidInputThreePlus extends AndroidInput implements OnGenericMotionListener {
    public static final String TAG=AndroidInputThreePlus.class.getName();

    ArrayList<OnGenericMotionListener> genericMotionListeners = new ArrayList();
    private final AndroidMouseHandler mouseHandler;

    private boolean interruptTouch=false;

    public AndroidInputThreePlus (Application activity, Context context, Object view, AndroidApplicationConfiguration config) {
        super(activity, context, view, config);
        // we hook into View, for LWPs we call onTouch below directly from
        // within the AndroidLivewallpaperEngine#onTouchEvent() method.
        if (view instanceof View) {
            View v = (View)view;
            v.setOnGenericMotionListener(this);
        }
        mouseHandler = new AndroidMouseHandler();
    }

    @Override
    public boolean onGenericMotion (View view, MotionEvent event) {
        timber.log.Timber.d("onGenericMotion"+view.toString());
        if (mouseHandler.onGenericMotion(event, this)) return true;
        for (int i = 0, n = genericMotionListeners.size(); i < n; i++)
            if (genericMotionListeners.get(i).onGenericMotion(view, event)) return true;
        return false;
    }

    public void addGenericMotionListener (OnGenericMotionListener listener) {
        genericMotionListeners.add(listener);
    }


    @Override
    public boolean onTouch (View view, MotionEvent event) {
        super.onTouch(view,event);
        //timber.log.Timber.d("onTouch interrupt:"+interruptTouch+"\t event:"+event.toString());
        return interruptTouch;
    }

    public void setInterruptTouch(boolean able){
        this.interruptTouch=able;
    }
}

